package chpt_05_01_adaptor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

/**
 * @author Ping
 * @date 2023/3/30 9:23
 **/

/**
 * Adaptor role in Adpater pattern by dynamic mechanism.
 */
@Component("moreSatetyEncoderDynamic")
public class MoreSafetyEncoderDynamic implements MoreSafetyEncoder {
    /**
     * key point:  compose the related encoder.
     */
    @Autowired
    @Qualifier("md5Encoder")
    private MD5Encoder encoder ;

    public void setEncoder(MD5Encoder encoder) {
        this.encoder = encoder;
    }

    @Override
    public String encodeInRound(String text, int round) throws Throwable {
        String ret = text ;
        for (int i=0; i<round; i++ ){

            ret = this.encoder.encode(ret) ;
        }
        return ret;
    }

    @Override
    public String encodeBySalt(String text, String salt) throws Throwable {
        return this.encoder.encode(text + salt);
    }
}
